﻿
using Red.Core;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System;

namespace Red.Controls
{
    /// <summary>
    ///     ToggleButton with icon
    /// </summary>
    public class RedToggleButton : ToggleButton
    {
        #region Data
        
        private object _tooltipLatch;
        
        #endregion Data

        #region Properties

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register( "Icon", typeof( Canvas ), typeof( RedToggleButton ) );

        public static readonly DependencyProperty CheckedIconProperty =
            DependencyProperty.Register( "CheckedIcon", typeof( Canvas ), typeof( RedToggleButton ) );

        public static readonly DependencyProperty CheckedToolTipProperty =
            DependencyProperty.Register( "CheckedTooltip", typeof( object ), typeof( RedToggleButton ) );
               
        /// <summary>
        ///     Icon displayed on button
        /// </summary>
        public Canvas Icon
        {
            get { return (Canvas)GetValue( IconProperty ); }
            set { SetValue( IconProperty, value ); }
        }

        /// <summary>
        ///     Icon displayed on button in checked state
        /// </summary>
        public Canvas CheckedIcon
        {
            get { return (Canvas)GetValue( CheckedIconProperty ); }
            set { SetValue( CheckedIconProperty, value ); }
        }

        /// <summary>
        ///     Tooltip displayed in checked state
        /// </summary>
        public object CheckedToolTip
        {
            get { return GetValue( CheckedToolTipProperty ); }
            set { SetValue( CheckedToolTipProperty, value ); }
        }

        #endregion Properties

        public RedToggleButton()
        {
            var icon = new RedIcon();
            icon.SetBinding( RedIcon.CanvasProperty, new Binding( "Icon" ) { Source = this } );
            icon.SetBinding( RedIcon.AlternateCanvasProperty, new Binding( "CheckedIcon" ) { Source = this } );
            icon.SetBinding( RedIcon.UseAlternateCanvasProperty, new Binding( "IsChecked" ) { Source = this } );
            AddChild( icon );
        }

        #region Event handlers

        protected override void OnPropertyChanged( DependencyPropertyChangedEventArgs e )
        {
            base.OnPropertyChanged( e );

            if ( e.Property == CommandProperty )
            {
                var cmd = Command as RoutedUICommand;
                if ( cmd != null )
                {
                    ToolTip = cmd.Text;
                }

                if ( Icon == null && cmd is RedCommand )
                {
                    Icon = ( (RedCommand)cmd ).Icon;
                }
            }
            else if ( e.Property == IsCheckedProperty )
            {
                if ( CheckedToolTip != null )
                {
                    if ( true.Equals( e.NewValue ) )
                    {
                        _tooltipLatch = ToolTip;
                        Dispatcher.BeginInvoke( (Action)delegate () 
                        {
                            ToolTip = CheckedToolTip;
                        } );
                    }
                    else if ( _tooltipLatch != null )
                    {
                        Dispatcher.BeginInvoke( (Action)delegate () 
                        {
                            ToolTip = _tooltipLatch;
                            _tooltipLatch = null;
                        } );
                    }
                }
            }
        }

        #endregion Event handlers
    }

}
